/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.recordsCenter.service.impl;

import me.zhengjie.modules.his.domain.InoutpatientType;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.recordsCenter.repository.InoutpatientTypeRepository;
import me.zhengjie.modules.his.recordsCenter.service.InoutpatientTypeService;
import me.zhengjie.modules.his.recordsCenter.service.dto.InoutpatientTypeDto;
import me.zhengjie.modules.his.recordsCenter.service.dto.InoutpatientTypeQueryCriteria;
import me.zhengjie.modules.his.recordsCenter.service.mapstruct.InoutpatientTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author his
* @date 2022-11-11
**/
@Service
@RequiredArgsConstructor
public class InoutpatientTypeServiceImpl implements InoutpatientTypeService {

    private final InoutpatientTypeRepository inoutpatientTypeRepository;
    private final InoutpatientTypeMapper inoutpatientTypeMapper;

    @Override
    public Map<String,Object> queryAll(InoutpatientTypeQueryCriteria criteria, Pageable pageable){
        Page<InoutpatientType> page = inoutpatientTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(inoutpatientTypeMapper::toDto));
    }

    @Override
    public List<InoutpatientTypeDto> queryAll(InoutpatientTypeQueryCriteria criteria){
        return inoutpatientTypeMapper.toDto(inoutpatientTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public InoutpatientTypeDto findById(Integer inoutpatientId) {
        InoutpatientType inoutpatientType = inoutpatientTypeRepository.findById(inoutpatientId).orElseGet(InoutpatientType::new);
        ValidationUtil.isNull(inoutpatientType.getInoutpatientId(),"InoutpatientType","inoutpatientId",inoutpatientId);
        return inoutpatientTypeMapper.toDto(inoutpatientType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InoutpatientTypeDto create(InoutpatientType resources) {
        return inoutpatientTypeMapper.toDto(inoutpatientTypeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InoutpatientType resources) {
        InoutpatientType inoutpatientType = inoutpatientTypeRepository.findById(resources.getInoutpatientId()).orElseGet(InoutpatientType::new);
        ValidationUtil.isNull( inoutpatientType.getInoutpatientId(),"InoutpatientType","id",resources.getInoutpatientId());
        inoutpatientType.copy(resources);
        inoutpatientTypeRepository.save(inoutpatientType);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer inoutpatientId : ids) {
            inoutpatientTypeRepository.deleteById(inoutpatientId);
        }
    }

    @Override
    public void download(List<InoutpatientTypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InoutpatientTypeDto inoutpatientType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目名称", inoutpatientType.getProjectName());
            map.put("单位", inoutpatientType.getUnit());
            map.put("项目分类", inoutpatientType.getBigprojectId());
            map.put("单价", inoutpatientType.getPrice());
            map.put("创建者", inoutpatientType.getCreateBy());
            map.put("更新者", inoutpatientType.getUpdateBy());
            map.put("创建日期", inoutpatientType.getCreateTime());
            map.put("更新时间", inoutpatientType.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}